{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "# 定义URL和文件路径\n",
    "url = 'http://10.48.2.250/result.html'\n",
    "excel_file_path = r'C:\\python作业\\Contest Result.xlsx'\n",
    "\n",
    "# 确保保存文件的目录存在\n",
    "os.makedirs(os.path.dirname(excel_file_path), exist_ok=True)\n",
    "\n",
    "# 发送HTTP请求获取HTML内容\n",
    "try:\n",
    "    response = requests.get(url)\n",
    "    response.raise_for_status()  # 检查请求是否成功\n",
    "    html_content = response.text\n",
    "    print(\"HTML content fetched successfully.\")\n",
    "except requests.RequestException as e:\n",
    "    print(f\"Error fetching the URL: {e}\")\n",
    "    exit(1)\n",
    "\n",
    "# 使用BeautifulSoup解析HTML\n",
    "try:\n",
    "    soup = BeautifulSoup(html_content, 'html.parser')\n",
    "    print(\"HTML content parsed successfully.\")\n",
    "except Exception as e:\n",
    "    print(f\"Error parsing HTML: {e}\")\n",
    "    exit(1)\n",
    "\n",
    "# 假设HTML中的数据在一个<table>标签内，我们需要找到这个表格\n",
    "# 注意：这里的 'table' 是一个示例选择器，你需要根据实际情况修改它\n",
    "# 如果表格有特定的 id 或 class，你可以使用 '#id' 或 '.class' 来选择\n",
    "table = soup.find('table')  # 修改为 soup.find('table', {'id': 'specific-id'}) 或其他选择器\n",
    "\n",
    "if table is None:\n",
    "    print(\"Error: No table found in the HTML file.\")\n",
    "    exit(1)\n",
    "else:\n",
    "    print(\"Table found successfully.\")\n",
    "\n",
    "# 提取表格中的行和列\n",
    "try:\n",
    "    rows = table.find_all('tr')\n",
    "    data = []\n",
    "    for row in rows:\n",
    "        cells = row.find_all(['td', 'th'])  # 提取单元格，包括表头和普通单元格\n",
    "        row_data = [cell.get_text(strip=True) for cell in cells]  # 获取单元格文本并去除前后空白\n",
    "        data.append(row_data)\n",
    "    print(\"Table data extracted successfully.\")\n",
    "except Exception as e:\n",
    "    print(f\"Error extracting table data: {e}\")\n",
    "    exit(1)\n",
    "\n",
    "# 创建DataFrame\n",
    "try:\n",
    "    df = pd.DataFrame(data[1:], columns=data[0])  # 假设第一行是列名，其余行是数据\n",
    "    print(\"DataFrame created successfully.\")\n",
    "except Exception as e:\n",
    "    print(f\"Error creating DataFrame: {e}\")\n",
    "    exit(1)\n",
    "\n",
    "# 将DataFrame保存为Excel文件\n",
    "try:\n",
    "    df.to_excel(excel_file_path, index=False)\n",
    "    print(f\"数据已成功保存到 {excel_file_path}\")\n",
    "except Exception as e:\n",
    "    print(f\"Error saving to Excel: {e}\")\n",
    "    exit(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
